<?php
/**
 * Developer
 * 
 * This class extends the DBObject class and represents a single row in the developer table
 * of the BugZapp database.
 * 
 * @package BugZapp
 * @author Mark Johnson
 * @copyright 2010
 * @version 1.0
 * @access public
 */
class Developer extends DBObject
{
	// load defaults
    var $did = 0;
    
    var $name = 'anonymous';
    var $email = 'anonymous@anonymous'; 
    
    /**
     * Developer::Developer()
     * 
     * This constructor sets up the necessary data that DBObject uses to make queries on the developer database
     * 
     * @return
     */
    public function Developer($data = NULL) 
    {        
        $this->table = 'developer';
        
        $this->id_name = 'did';
        
        $this->fields = array(
            'name' => "'%s'",
            'email' => "'%s'"
        );
        
    	parent::DBObject($data);
    }
    
    /**
     * Developer::schema()
     * 
     * @return
     */
    public static function schema()
    {
        return 'CREATE TABLE IF NOT EXISTS `developer` (
    			  `did` int(10) unsigned NOT NULL AUTO_INCREMENT,
    			  `name` varchar(45) NOT NULL,
    			  `email` varchar(64) NOT NULL,
    			  PRIMARY KEY (`did`),
    			  UNIQUE KEY `did_UNIQUE` (`did`),
    			  UNIQUE KEY `email_UNIQUE` (`email`)
    			) ENGINE=InnoDB DEFAULT CHARSET=utf8';
    }
    
    /**
     * Developer::install()
     * 
     * @return
     */
    public static function install()
    {
        db_query('DROP TABLE IF EXISTS developer');
        $result = db_query(Developer::schema());
        
        if ($result !== FALSE)
    	{			
    		system_set_message('The <code>developer</code> table was successfully created.');
    		
    		db_query("INSERT INTO developer (name, email) VALUES ('anonymous', 'anonymous@anonymous')");
    		db_query("UPDATE developer SET did=0 WHERE email='%s'", 'anonymous@anonymous');
            db_query('ALTER TABLE developer AUTO_INCREMENT=1');
            
            return TRUE;
    	}
    	else
    	{
    		system_set_message('The <code>developer</code> table was not created.');
    		
    		return FALSE;
    	}
    }
    
    /**
     * Developer::uninstall()
     * 
     * @return
     */
    public static function uninstall()
    {
   	    $result = db_query('DROP TABLE IF EXISTS developer');
    	if ($result !== FALSE)
    	{			
    		system_set_message('The <code>developer</code> table was successfully removed.');
    		
    		return TRUE;
    	}
    	else
    	{
    		system_set_message('The <code>developer</code> table was not removed.');
    		
    		return FALSE;
    	}
    }
}
